<?php
namespace Futures\Controller;
use Think\Controller;
use Think\FuturesPage;
/**
 *  分销中心控制器
 */
class CommissionController extends BaseController{

    public function index(){
        $user_auth= cookie('user_auth');
        $user_id=$user_auth['uid'];
        //$user_id = 426;
        //获取昵称
        $user = M('Follow')->field('nickname,money,headimgurl')->where(array('id'=>$user_id))->find();
        $this->assign('user',$user);

        //获取我的经纪人总数（三级）
        //$agent_where['_string'] = "mobile!='' and ((route like '{$user_id}') or (route regexp '.*,{$user_id}$') or (route regexp '{$user_id},([0-9]*)$') or (route regexp '.*,{$user_id},([0-9]*)$') or (route regexp '{$user_id},([0-9]*),([0-9]*)$') or (route regexp '.*,{$user_id},([0-9]*),([0-9]*)$'))";
		$agent_where['_string'] = "mobile!='' and ((route like '{$user_id}') or (route regexp '.*,{$user_id}$'))";
        $agen_count = M('follow')->where($agent_where)->count();
        $this->assign('agen_count',$agen_count);
        //获取已分配佣金
        $has = M('FuturesCommission')->where(array('commission_user_id'=>$user_id,'status'=>1))->sum('commission');
        if(!$has) $has = 0.00;
        //获取未分配佣金
        $nohas = M('FuturesCommission')->where(array('commission_user_id'=>$user_id,'status'=>0))->sum('commission');
        if(!$nohas) $nohas = 0.00;
        $this->assign('has',$has);
        $this->assign('nohas',$nohas);
        $this->display();
    }

    public function my_agent(){
        $user_auth= cookie('user_auth');
        $user_id=$user_auth['uid'];
	if($user_id==7387){
		//$user_id=7401;
	}
        $level = 1;
        if(isset($_GET['level'])){
            $level = I('get.level');
        }
        $agent_where_one['_string'] = "mobile!='' and ((route like '{$user_id}') or (route regexp '.*,{$user_id}$'))";
        //$agent_where_one['_string'] = "(route like '{$user_id}') or (route like '.*,{$user_id}')";
        $count_one = M('follow')->where($agent_where_one)->count();
        $agent_where_two['_string'] = "mobile!='' and ((route REGEXP '.*,{$user_id},([0-9]*)$') or (route REGEXP '{$user_id},([0-9]*)$'))";
        $count_two = M('follow')->where($agent_where_two)->count();
        $agent_where_three['_string'] = "mobile!='' and ((route regexp '.*,{$user_id},([0-9]*),([0-9]*)$') or (route regexp '{$user_id},([0-9]*),([0-9]*)$'))";
        $count_three = M('follow')->where($agent_where_three)->count();
        if(!$level || $level==1){
            $count  = M('follow')->where($agent_where_one)->count();
            //$agents = M('follow')->where($agent_where_one)->select();
            $where = $agent_where_one;
        }elseif($level==2){
            //$agents = M('follow')->where($agent_where_two)->select();
            $count  = M('follow')->where($agent_where_two)->count();
            $where = $agent_where_two;
        }elseif($level==3){
            //$agents = M('follow')->where($agent_where_three)->select();
            $count  = M('follow')->where($agent_where_three)->count();
            $where = $agent_where_three;
        }
        $Page  = new FuturesPage($count,10);
        $show = $Page->show();// 分页显示输出
        $per_num    = 10;//每页条数    自定义
        $pages      = ceil($count/$per_num);//总页数
        $this->assign('page',$show);
        $agents = M('follow')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        foreach($agents as $k=>$v){
            $commission = M('FuturesCommission')->where(array('commission_user_id'=>$v['id']))->sum('commission');
            if(empty($commission)) $commission=0.00;
            $agents[$k]['commission'] = $commission;
        }
        $this->assign('count_one',$count_one);
        $this->assign('count_two',$count_two);
        $this->assign('count_three',$count_three);
        $this->assign('agents',$agents);
        $this->assign('level',$level);
        $this->display();
    }

    public function detail(){
        /*获取用户*/
        $user_auth= cookie('user_auth');
        $user_id=$user_auth['uid'];
        //$user_id = 426;

        $record = M('FuturesCommission');
        $where['commission_user_id'] = $user_id;
        if(isset($_GET['status'])){
            $status = I('get.status');
            $where['status'] = $status;
            $this->assign('status',$status);
        }else{
            $this->assign('status',-1);
        }
        $count      = $record->where($where)->count();// 查询满足要求的总记录数
        $Page       = new FuturesPage($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //$Page->lastSuffix = false;//最后一页不显示为总页数
        $show       = $Page->show();// 分页显示输出
        $per_num    = 10;//每页条数    自定义
        $pages      = ceil($count/$per_num);//总页数
        $this->assign('page',$show);
        $refer = $record->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $userwhere['id']=$user_id;
        $userData=M('follow')->where($userwhere)->find();
        $this->assign("refer",$refer);
       //print_r($refer);exit;
        $url = U('Commission/detail','','');
        $this->assign('url',$url);
        $this->assign('userData',$userData);
        $this->display();
    }
}
